//  我的-部门管理-面试公告设置
const baseUrl='https://gsdse.cn/api/KJRecruit';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    department: '',
    publicAnnouncement: '',
    announcement: ''
  },

  //--------------------------------------
  //methods

  inputContent(e) {
    // console.log(e);
    this.setData({
      content: e.detail.value
    });
    //console.log(e.detail.value)

  },
  save(e) {
    
    if(this.data.content != ''){
          wx.request({
      url: baseUrl+'/updateDepartment',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "departmentId": this.data.personInfo.departmentId,
        "applyNumber": this.data.department.applyNumber,
        "identification": this.data.department.identification,
        "_key": this.data.department._key,
        "notice": this.data.content,
        "congratulation": this.data.department.congratulation
      },

      success: (res) => {
        if (res.data.code == 0) {
          wx.showToast({
            icon: 'none',
            title: '修改成功',
          })
        }
        // getApp().getDeparment();
        // console.log(res.data)
      }
    })
    }else{
      wx.showToast({
        icon:'none',
        title: '当前内容为空',
      })
    }


  },
  // save(e) {

  //   this.setData({
  //     publicAnnouncement: this.data.content
  //   });
  //   if (this.data.publicAnnouncement === '') {
  //     console.log('输入为空');
  //     let delshow = !this.data.delshow
  //     console.log(delshow)
  //     this.setData({
  //       delshow
  //     })
  //     return false
  //   } else {
  //     db.collection('Users').where({
  //       _openid: getApp()._openid
  //     }).get().then(res => {
  //       this.setData({
  //         department: res.data[0].department
  //       });
  //       console.log(this.data.department);
  //       console.log(this);
  //       db.collection('boardcast').where({
  //           departmentName: this.data.department
  //         }).update({
  //           data: {
  //             publicAnnouncement: this.data.content
  //           },

  //           success: res => {
  //             console.log(res);
  //             if (res.stats.updated) {

  //               wx.hideLoading();
  //               wx.showToast({
  //                 title: '保存成功',
  //               });



  //             } else {

  //               wx.showToast({
  //                 icon: 'none',
  //                 title: '与原信息一致，无需更改',
  //               })
  //             }


  //           },
  //           fail: res => {
  //             wx.hideLoading();
  //             console.log(res);
  //             wx.showToast({
  //               icon: 'none',
  //               title: '保存失败',
  //             })
  //           }
  //         }),
  //         db.collection('congratulations').where({
  //           departmentName: getApp().personInfo.department
  //         }).get().then(
  //           res => {
  //             console.log(res);
  //             this.setData({
  //               announcement: res.data[0].publicAnnouncement,
  //               content: res.data[0].publicAnnouncement,
  //               publicAnnouncement: res.data[0].publicAnnouncement
  //             });
  //           }
  //         )
  //       console.log(this);
  //     })

  //   }


  // },
  cannel() {
    this.setData({
      delshow: false
    })
  },
  sure() {
    db.collection('Users').where({
      _openid: getApp()._openid
    }).get().then(res => {
      this.setData({
        department: res.data[0].department
      });
      console.log(this.data.department);
      console.log(this);
      db.collection('boardcast').where({
          departmentName: this.data.department
        }).update({
          data: {
            publicAnnouncement: ''
          },

          success: res => {
            console.log(res);
            if (res.stats.updated) {

              wx.hideLoading();
              wx.showToast({
                title: '保存成功',
              });
            } else {

              wx.showToast({
                icon: 'none',
                title: '与原信息一致，无需更改',
              })
            }

            this.setData({
              delshow: false
            })
          },
          fail: res => {
            wx.hideLoading();
            console.log(res);
            wx.showToast({
              title: '保存失败',
            })
          }
        }),
        console.log(this);
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      //获取全局变量personInfo和department
    this.setData({
      personInfo: app.data.personInfo,
      department: app.data.department
    })
    console.log("onLoadDepartment",this.data.department);
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      personInfo: app.data.personInfo,
      department: app.data.department,
      content: app.data.department.notice
    })
    console.log("onShowDepartment",this.data.department)
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})